/*
 * Map.cpp
 *
 * 
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses 
 */

#include "Map.h"
#include <cstdlib>
#include <ctime>
#include "ModelConstants.h"

using std::vector;
using std::pair;

Map::Map(int width, int height) {
    this->width = width;
    this->height = height;
    tiles.resize(width * height);
}

int Map::getWidth() {
    return width;
}

int Map::getHeight() {
    return height;
}

bool Map::isValidPosition(int x, int y) {
    if ((x < 0) || (x >= width)) {
        return false;
    }
    if ((y < 0) || (y >= height)) {
        return false;
    }
    return true;
}

bool Map::insertObject(MapObject* object, int x, int y) {
    if (! isValidPosition(x, y)) {
        return false;
    }
    if (! object->hasEnteredTheMap()) {
        notify(OBJECT_ENTERED_MAP, object);
        object->setEnteredTheMap(true);
    }
    int index = getIndexFromPos(x, y);
    return tiles[index].insertObject(object);
}

bool Map::dropObject(MapObject* object, int x, int y) {
    if (! isValidPosition(x, y)) {
        return false;
    }
    int index = getIndexFromPos(x, y);
    if (tiles[index].dropObject(object)) {
        if (! object->hasEnteredTheMap()) {
            notify(OBJECT_ENTERED_MAP, object);
            object->setEnteredTheMap(true);
        }
        return true;
    }
    return false;
}

MapObject* Map::getObject(int x, int y) {
    if (! isValidPosition(x, y)) {
        return 0;
    }
    int index = getIndexFromPos(x, y);
    return tiles[index].getObject();
}

MapObject* Map::getDropedObject(int x, int y) {
    if (! isValidPosition(x, y)) {
        return 0;
    }
    int index = getIndexFromPos(x, y);
    return tiles[index].getDropedObject();
}

MapObject* Map::removeObject(int x, int y) {
    if (! isValidPosition(x, y)) {
        return 0;
    }
    int index = getIndexFromPos(x, y);
    return tiles[index].removeObject();
}

bool Map::objectDropedOn(int x, int y) {
    if (! isValidPosition(x, y)) {
        return false;
    }
    int index = getIndexFromPos(x, y);
    return tiles[index].objectDropedOn();
}

MapObject* Map::removeDropedObject(int x, int y) {
    if (! isValidPosition(x, y)) {
        return 0;
    }
    int index = getIndexFromPos(x, y);
    return tiles[index].removeDropedObject();
}

void Map::applyEffect(MapEffect* effect, int x, int y) {
    if (! isValidPosition(x, y)) {
        return;
    }
    int index = getIndexFromPos(x, y);
    tiles[index].applyEffect(effect);
}

void Map::addBombermanSpawn(int x, int y) {
    bomberman_spawns.push_back(pair<int, int>(x, y));
}

vector<pair<int, int> >& Map::getBombermanSpawns() {
    return bomberman_spawns;
}

pair<int, int> Map::getRandomBombermanSpawn() {
    srand(time(NULL));
    int pos = rand() % bomberman_spawns.size();
    return bomberman_spawns[pos];
}

bool Map::tileIsOccupied(int x, int y) {
    if (! isValidPosition(x, y)) {
        return true;
    }
    int index = getIndexFromPos(x, y);
    return tiles[index].isOccupied();
}

Map::~Map() {
    tiles.clear();
}

int Map::getIndexFromPos(int x, int y) {
    return (x + y * width);
}
